package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.Text;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringUsersListView;
import com.cloudsigns.factorymanager2.engineering.adapter.PopWindowAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.UserInfoBean;
import com.cloudsigns.factorymanager2.engineering.customview.SpinerPopWindow;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringUserListPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 工程 工单管理 工单分派 工程安装 编辑派工信息
 * Created by tony on 2018/2/2.
 */

public class EditDispatchInformationForEngineeringInstallationOrderActivity extends Activity implements View.OnClickListener, IEngineeringUsersListView, AdapterView.OnItemClickListener {
    private View rl_back;
    private TextView tv_title;
    private RadioGroup rg_associated_size;
    private RadioButton rb_inner_dispatch;
    private ImageView img_inner_right;
    private TextView edt_inner_install_docking_colon, edt_external_dispatch_company, edt_external_install_docking_colon;
    private Button btn_upload;
    private View ll_inner_install_docking_colon, rl_external_dispatch;
    private String dispatch_way = "内派";
    private CommonEnum.ROLECATEGORY rolecategory;
    private List<UserInfoBean> userInfoBeanList = new ArrayList<UserInfoBean>();
    private List<String> popStrLs = new ArrayList<String>();
    private SpinerPopWindow<String> mSpinerPopWindow;
    private PopWindowAdapter popWindowAdapter;
    private UserInfoBean userInfoBean;
    //    private String id = "", name = "";
    private final String TAG = "EditDispatchInformationForEngineeringInstallationOrderActivity";
    private EngineeringUserListPresenter engineeringUserListPresenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_edit_dispatch_information_for_engineering_installation_order);

        engineeringUserListPresenter = new EngineeringUserListPresenter(this);
        init();
        if (null != getIntent()) {
            rolecategory = (CommonEnum.ROLECATEGORY) getIntent().getSerializableExtra(ConstantUtil.KEY_1);
            engineeringUserListPresenter.getUserList(rolecategory);

        }


    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rg_associated_size = findViewById(R.id.rg_associated_size);
        rb_inner_dispatch = findViewById(R.id.rb_inner_dispatch);
        edt_inner_install_docking_colon = findViewById(R.id.edt_inner_install_docking_colon);
        edt_external_dispatch_company = findViewById(R.id.edt_external_dispatch_company);
        edt_external_install_docking_colon = findViewById(R.id.edt_external_install_docking_colon);
        btn_upload = findViewById(R.id.btn_upload);
        ll_inner_install_docking_colon = findViewById(R.id.ll_inner_install_docking_colon);
        rl_external_dispatch = findViewById(R.id.rl_external_dispatch);
        img_inner_right = findViewById(R.id.img_inner_right);
        tv_title.setText(getResources().getText(R.string.edit_dispatch_information));
        rl_back.setOnClickListener(this);
        btn_upload.setOnClickListener(this);
        rb_inner_dispatch.setChecked(true);
        img_inner_right.setOnClickListener(this);
        edt_inner_install_docking_colon.setOnClickListener(this);

        rg_associated_size.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) {
                if (id == R.id.rb_inner_dispatch) {
                    dispatch_way = "内派";
                    showDispatchContent();
                } else if (id == R.id.rb_external_dispatch) {
                    dispatch_way = "外派";
                    showDispatchContent();
                }

            }
        });

        rb_inner_dispatch.setChecked(true);


        mSpinerPopWindow = new SpinerPopWindow<String>(this, this);
        popWindowAdapter = new PopWindowAdapter(this, TAG);
        popWindowAdapter.setData(popStrLs);
        mSpinerPopWindow.setAdapter(popWindowAdapter);
    }

    private void showDispatchContent() {
        if (dispatch_way.equals("内派")) {
            ll_inner_install_docking_colon.setVisibility(View.VISIBLE);
            rl_external_dispatch.setVisibility(View.GONE);
        } else if (dispatch_way.equals("外派")) {
            ll_inner_install_docking_colon.setVisibility(View.GONE);
            rl_external_dispatch.setVisibility(View.VISIBLE);
        }
    }

    private void upload() {
        Intent mIntent = new Intent();
        mIntent.putExtra("dispatch_way", dispatch_way);
        if (dispatch_way.equals("内派")) {
            mIntent.putExtra(ConstantUtil.KEY_1, userInfoBean);
        } else if (dispatch_way.equals("外派")) {
            mIntent.putExtra(ConstantUtil.KEY_1, edt_external_install_docking_colon.getText().toString().trim());
            mIntent.putExtra(ConstantUtil.KEY_2, edt_external_dispatch_company.getText().toString().trim());
        }
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);

//        Toast.makeText(this, "上传成功！", Toast.LENGTH_SHORT).show();
        finish();
    }

    private void showInnerInstallDockingPopWindow() {
        mSpinerPopWindow.setWidth(edt_inner_install_docking_colon.getWidth());
        mSpinerPopWindow.showAsDropDown(edt_inner_install_docking_colon);
    }


    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_upload:
                upload();
                break;
            case R.id.img_inner_right:
                showInnerInstallDockingPopWindow();
                break;

        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        edt_inner_install_docking_colon.setText(popStrLs.get(i));
        userInfoBean = userInfoBeanList.get(i);
        mSpinerPopWindow.dismiss();

    }

    @Override
    public void getDataSuccessed(List<UserInfoBean> dataLs) {
        if (null != dataLs) {
            userInfoBeanList.clear();
            userInfoBeanList.addAll(dataLs);
            for (UserInfoBean userInfoBean : userInfoBeanList) {
                popStrLs.add(userInfoBean.getName() + ":" + userInfoBean.getId());
            }

            popWindowAdapter.setData(popStrLs);
            popWindowAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSpinerPopWindow != null) {
            mSpinerPopWindow.dismiss();
            mSpinerPopWindow = null;
        }


    }
}
